package com.ptmall.app;

import android.annotation.SuppressLint;
import android.app.Application;
import android.app.NotificationManager;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Build;
import android.os.Environment;
import android.support.multidex.MultiDex;
import android.util.Log;

//import com.alibaba.sdk.android.push.CloudPushService;
//import com.alibaba.sdk.android.push.CommonCallback;
//import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.baidu.mapapi.SDKInitializer;
import com.facebook.stetho.Stetho;
import com.ptmall.app.bean.model.db.Account;
import com.ptmall.app.bean.model.db.dao.DaoMaster;
import com.ptmall.app.bean.model.db.dao.DaoSession;

import java.io.File;

/**
 * Created by Administrator on 2017/2/20 0020.
 * <p>
 * <p>
 * >><br>┏┓　　　┏┓
 * >><br>┏┛┻━━━┛┻┓
 * >><br>┃　　　　　　　┃
 * >><br>┃　　　━　　　┃
 * >><br>┃　┳┛　┗┳　┃
 * >><br>┃　　　　　　　┃
 * >><br>┃　　　┻　　　┃
 * >><br>┃　　　　　　　┃
 * >><br>┗━┓　　　┏━┛
 * >><br>┃　　　┃  神兽保佑
 * >><br>┃　　　┃  代码无BUG！
 * >><br>┃　　　┗━━━┓
 * >><br>┃　　　　　　　┣┓
 * >><br>┃　　　　　　　┏┛
 * >><br>┗┓┓┏━┳┓┏┛
 * >><br> ┃┫┫　┃┫┫
 * >><br> ┗┻┛　┗┻┛
 */

public class App extends Application {


    private Account mAccount = null;//当前登录用户
    private static final String TAG = "App";
    private static App sInstance;

    private DaoMaster.DevOpenHelper mHelper;
    private SQLiteDatabase db;
    private DaoMaster mDaoMaster;
    private DaoSession mDaoSession;

    @Override
    public void onCreate() {
        super.onCreate();
        sInstance = this;
        AppApplication.init(this);
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(this);
        Stetho.initializeWithDefaults(this);
        SDKInitializer.initialize(this);//百度地图初始化
        initCloudChannel(this);
//        setDatabase();

    }

    public synchronized static App getInstance() {
        if (null == sInstance) {
            sInstance = new App();
        }
        return sInstance;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        //分包
        MultiDex.install(this);

    }

    private void setDatabase() {
        mHelper = new DaoMaster.DevOpenHelper(this, "ptmall.db", null);
        db = mHelper.getWritableDatabase();
        mDaoMaster = new DaoMaster(db);
        mDaoSession = mDaoMaster.newSession();
    }


    public DaoSession getDaoSession() {
        return mDaoSession;
    }

    public SQLiteDatabase getDb() {
        return db;
    }

    //退出登陆时 调用下改方法清空记录
    public void clearLoginAccount() {
        this.mAccount = null;
    }

    public String getCachePath() {
        File cacheDir;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            cacheDir = sInstance.getExternalCacheDir();
        } else {
            cacheDir = sInstance.getCacheDir();
        }
        if (!cacheDir.exists()) {
            cacheDir.mkdirs();
        }
        return cacheDir.getAbsolutePath();

    }
    /**
     * 初始化云推送通道
     * @param applicationContext
     */
    private void initCloudChannel(Context applicationContext) {

        PushServiceFactory.init(applicationContext);
        CloudPushService pushService = PushServiceFactory.getCloudPushService();
        pushService.register(applicationContext, new CommonCallback() {
            @Override
            public void onSuccess(String response) {
                Log.d(TAG, "init cloudchannel success");
            }
            @Override
            public void onFailed(String errorCode, String errorMessage) {
                Log.d(TAG, "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
            }
        });
    }
}
